package cn.qyd.javathread;

/**
 * @author 邱运铎
 * 该例子是让thread1和thread2在主线程下执行
 * 但在thread1线程运行过程中在指定时刻加入thread3，并调用thread3.join，
 * 让thread0需要再thread3执行完后在接着执行，但在这个过程并不影响主线程和thread2的执行
 *
 * @date 2024-03-14 12:33
 */
public class ImRunableThreadDemo implements Runnable {
    @Override
    public void run() {
        for (int i=0; i < 100; i++) {
            if (i == 10) {
                Thread thread3 = new Thread(new RunnableThreadDemo());
                thread3.start();
                try {
                    thread3.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName() + "------------------" + i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new ImRunableThreadDemo();
        Runnable runnable2 = new RunnableThreadDemo();
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable2);

        thread1.start();
        //调用join方法，使得主线程需要等待thread1执行完毕后，才执行thread2
//        thread1.join();
        thread2.start();
    }
}
